package com.cedifie.lendapp.analysis

import com.cedifie.analysis.AnalysisBuilder
import com.cedifie.analysis.AnalyticsUtil
import com.cedifie.lendapp.BuildConfig
import com.cedifie.lendapp.MyApplication
import com.cedifie.lendapp.config.CedConfigs
import com.cedifie.lendapp.dv_data.CedDvManage
import com.cedifie.lendapp.http.ApiService
import com.cedifie.lendapp.http.ApiService.addCommonParams
import com.cedifie.lendapp.http.HttpHelper
import com.cedifie.lendapp.http.IHttpCallback
import com.cedifie.lendapp.utils.helper.SharedHelper
import org.json.JSONObject


/**
 * TODO: Analysis Lib 初始化时机
 */
object TestAnalysisInit {

    /**
     * TODO: APP启动时，初始化一下 analysis lib
     */
    fun initAnalysisLibAtAppLaunch() {
        AnalysisBuilder.product = CedConfigs.CED_APPNAME
        AnalysisBuilder.appVersion = BuildConfig.VERSION_CODE.toString()
        AnalysisBuilder.appPckName = CedConfigs.CED_PACKAGENANME

        AnalysisBuilder.aesKey = CedConfigs.CED_AES_KEY
        AnalysisBuilder.aesIv = CedConfigs.CED_AES_IV

        AnalysisBuilder.host = ApiService.CED_BASE_URL
        AnalysisBuilder.zone = ""
        AnalysisBuilder.pathCommon = ApiService.API_URL.CED_API_COMMON_URL

        AnalysisBuilder.pathGoogleInstall = ApiService.ANALYSIS_PATH.CED_ANALYSIS_GOOGLE_INSTALL_PATH
        AnalysisBuilder.pathAdjustInstall = ApiService.ANALYSIS_PATH.CED_ANALYSIS_ADJUST_INSTALL_PATH
        AnalysisBuilder.pathFacebookInstall = ApiService.ANALYSIS_PATH.CED_ANALYSIS_FACEBOOK_INSTALL_PATH
        AnalysisBuilder.pathFirebaseInstall = ApiService.ANALYSIS_PATH.CED_ANALYSIS_FIREBASE_INFO_PATH

        AnalyticsUtil.init(MyApplication.app)
    }

    /**
     * TODO: 成功获取地区转发路径时，初始化一下 analysis lib
     */
    fun initAnalysisLibAtZoneSuccess() {
        AnalysisBuilder.host = ApiService.CED_BASE_URL
        AnalysisBuilder.zone = SharedHelper.getCedSPString(SharedHelper.CED_SPKEY.CEDIFIE_TIMEZONE)
        AnalyticsUtil.init(MyApplication.app)
    }

    /**
     * TODO: 成功登录时，初始化一下 analysis lib
     */
    fun initAnalysisLibAtLoginSuccess() {
        AnalysisBuilder.token = SharedHelper.getCedSPString(SharedHelper.CED_SPKEY.CEDIFIE_TOKEN)
        AnalysisBuilder.userId = SharedHelper.getCedSPString(SharedHelper.CED_SPKEY.CEDIFIE_USERID)
        AnalyticsUtil.init(MyApplication.app)
    }
}

/**
 * TODO: Analysis Lib 打点时机
 */
object TestAnalysisAddEvent {

    /**
     * TODO: 在调用登录接口，并成功返回时，如果 type=="REGISTER"，则调用注册事件打点
     */
    fun addRegisterEvent() {
        AnalyticsUtil.addRegisterEvent()
    }

    /**
     * TODO: 在调用提交贷款申请接口，并成功返回时，则调用申请事件打点
     */
    fun addApplyEvent() {
        AnalyticsUtil.addApplyEvent()
    }
}

/**
 * TODO: Data Lib 调用时机
 */
object TestDataLibInvoke {

    /**
     * TODO: 在首页每次点击申请时（在已登录的前提下），调用一下 data lib 的上报
     * 获取设备数据上传状态
     */
    fun loadCedPhoneInfoListAction(){
        val httpUrl = ApiService.getHttpUrl(ApiService.API_URL.CED_API_COMMON_URL)
        val params = JSONObject().addCommonParams().apply {
            put("path", ApiService.API_PATH.CED_API_DEVICE_INFO_TYPE_PATH)
        }
        HttpHelper.postCedAction(httpUrl,params,object : IHttpCallback {
            override fun onRequestSuccess(data: JSONObject) {
                when(data.optInt("code")) {
                    0 -> {//成功
                        val obj = data.optJSONObject("obj")
                        obj?.let {
                            val deviceInfoTypes = obj.optString("deviceInfoTypes")
                            CedDvManage.updateCedDvData(MyApplication.app,deviceInfoTypes)
                        }
                    }
                }
            }

            override fun onRequestError(error: JSONObject) {
            }

            override fun onRequestFinish() {
            }
        })
    }
}